﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Meqs.Entity;
using Meqs.Entity.Parameters;
using Meqs.Logic;
using Meqs.Logic.Interface;
using Meqs.Utilities;
using System.Web.Script.Serialization;

public partial class TeacherEval_Student_CauseTeacherEval_LuoFangeEvalTeacher : ScoreInputBasePage
{
    protected int historyId = 0;
    protected int teacherId = 0;
    protected int year = 0;
    protected int month = 0;
    private int _tableWidth;
    protected string ReportTitle
    {
        get
        {
            string title = "{0}{1} {2}{3}学生评教报表";

            int schoolYear = SchoolYear;
            int term = Term;
            if (SchoolYear == -1 || Term == -1)
            {
                ISchoolInfoLogic schLogic = Container<ISchoolInfoLogic>.Instance;
                SchoolInfo sch = schLogic.FindBySchoolMark(SchoolMark);
                schoolYear = sch.SchoolYear;
                term = sch.SchoolTerm;
            }
            string termStr = term == 1 ? "上学期" : "下学期";
            return string.Format(title, Common.ConvertSchoolYear(schoolYear), termStr, Cause == null ? "" : Cause.CauseName, Common.ReportDateType(3));
        }
    }

    protected string ReportSubTitle
    {
        get
        {
            string title = "学年：{1}";

            string strTerm = "";
            if (Term == 1)
                strTerm = "上学期";
            else
                strTerm = "下学期";

            return Year + "年" + strTerm;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string s = Request.Params["HistoryId"];
            if (!string.IsNullOrEmpty(s))
                int.TryParse(s, out historyId);

            s = Request.Params["TeacherId"];
            if (!string.IsNullOrEmpty(s))
                int.TryParse(s, out teacherId);

            s = Request.Params["Year"];
            if (!string.IsNullOrEmpty(s))
                int.TryParse(s, out year);

            s = Request.Params["Month"];
            if (!string.IsNullOrEmpty(s))
                int.TryParse(s, out month);

            BindTable();
        }
    }



    #region 绑定表格
    protected void BindTable()
    {
        int tmonth = 0;
        if (Month <= 0)
        {
            List<TermInfo> termInfo = (List<TermInfo>) Container<ITermInfoLogic>.
                                        Instance.FindTermInfo(SchoolMark, SchoolYear, Term);
            tmonth = termInfo[0].StartDate.Value.Month;
        }
        else
            tmonth = Month;

        _tableWidth = 0;

        IList<TeacherEvalType> list;

        if (SchoolYear == -1 || Term == -1)
        {
            SchoolInfo schoolInfo = Container<ISchoolInfoLogic>.Instance.
                FindBySchoolMark(SchoolMark);
            list = Container<ITeacherEvalTypeLogic>.Instance.
                GetTeachHisType(SchoolMark, schoolInfo.SchoolYear, schoolInfo.SchoolTerm, HistoryId, "Teacher");
        }
        else
        {
            list = Container<ITeacherEvalTypeLogic>.Instance.
                GetTeachHisType(SchoolMark, SchoolYear, Term, HistoryId, "Teacher");
        }

        int managerId = Common.GetUserInfo().UID;
        IList<TeacherInputEntity> sieList = Container<ITeacherEvalDetailLogic>.Instance.
            GetTeacherInputEntityByStudent(HistoryId, EvalTeacherID, managerId, Year, tmonth, SchoolYear, Term, SchoolMark);
        //IList<ParamVal> pvList = new List<ParamVal>();
        //foreach (TeacherInputEntity item in sieList)
        //{
        //    ParamVal pv = new ParamVal();
        //    pv.ItemID = item.ItemID;
        //    if (!_paramVals.Contains(pv)) continue;
        //    ParamVal p = _paramVals[_paramVals.IndexOf(pv)];
        //    p.Type = item.Type;
        //    p.Param = item.Param;
        //    p.Score = item.Score;
        //    p.Operator = item.Operator;
        //    pvList.Add(p);
        //}
        //ListSort<ParamVal> sort = new ListSort<ParamVal>(pvList, "Index", true);
        //pvList = sort.Sort();

        TableHeaderRow thr = new TableHeaderRow();
        TableHeaderCell thc = new TableHeaderCell();
        thc.Text = "内容";
        thr.Cells.Add(thc);

        thc = new TableHeaderCell();
        thc.Text = "评价";
        thr.Cells.Add(thc);
        tbResult.Rows.Add(thr);

        foreach (TeacherEvalType type in list)
        {
            TableRow tr = new TableRow();
            IList<TeacherEvalItem> miList = Container<ITeacherEvalItemLogic>.Instance.
                GetItemByType(SchoolMark, type.RID);
            TableCell tc = new TableCell();
            tc.Text = miList[0].Caption;
            tr.Cells.Add(tc);
            tr.ID = miList[0].RID.ToString();
            tr.Attributes.Add("itemID", miList[0].RID.ToString());
            tr.Attributes.Add("teachID", teacherId.ToString());

            TeacherInputEntity tie = null;
            foreach (TeacherInputEntity val in sieList)
            {
                if (val.ItemID == miList[0].RID)
                {
                    tie = val;
                    break;
                }
            }

            tc = new TableCell();
            RadioButton rb = new RadioButton();
            rb.Text = "是";
            rb.GroupName = miList[0].RID.ToString();
            if (tie != null && tie.Param > 0)
            { 
                rb.Checked = true;
            }
            tc.Controls.Add(rb);
            rb = new RadioButton();
            rb.Text = "否";
            rb.GroupName = miList[0].RID.ToString();
            if (tie != null && tie.Param == 0)
            {
                rb.Checked = true;
            }
            tc.Controls.Add(rb);
            tr.Cells.Add(tc);
            tbResult.Rows.Add(tr);
        }
        TableRow remarkrow = new TableRow();
        TableCell rc = new TableCell();
        rc.ColumnSpan = 2;
        TextBox remark = new TextBox();
        Label lb = new Label();
        lb.Text = "你想说的话:";
        lb.Style["text-align"] = "left";
        lb.Style["width"] = "100%";
        //remark.Style["border-top"] = "0px";
        remark.Style["width"] = "100%";
        TeacherEvalRemark mr = Container<ITeacherEvalRemarkLogic>.Instance.
            GetRemarkByTeacher(HistoryId, EvalTeacherID, managerId, 4, Year, tmonth, SchoolYear, Term, SchoolMark);
        if(mr != null)
            remark.Text = mr.Data;
        rc.Controls.Add(lb);
        rc.Controls.Add(remark);
        remarkrow.Cells.Add(rc);
        tbResult.Rows.Add(remarkrow);
    }


    #endregion

    /// <summary>
    /// 保存单据
    /// </summary>
    /// <param name="jsonStr"></param>
    /// <param name="causeID"></param>
    /// <param name="clsID"></param>
    /// <param name="schoolYear"></param>
    /// <param name="term"></param>
    /// <param name="year"></param>
    /// <param name="month"></param>
    /// <returns></returns>
    [WebMethod]
    public static bool SaveScoreInput(int historyId, string jsonStr, int schoolYear, int term, int year, int month)
    {
        try
        {
            IList<VItems> list = ConvertJson(jsonStr);

            int? syear = schoolYear == -1 ? (int?)null : schoolYear;
            int? sterm = term == -1 ? (int?)null : term;

            ITermInfoLogic termLogic = Container<ITermInfoLogic>.Instance;

            List<TermInfo> termInfo = (List<TermInfo>)termLogic.FindTermInfo(SchoolMark, schoolYear, term);
            month = termInfo[0].StartDate.Value.Month;
            ITeacherEvalSubmitRecordLogic scesrLogic = Container<ITeacherEvalSubmitRecordLogic>.Instance;

            return scesrLogic.SaveOrUpdateRecord(list, historyId, 4, year, month, syear, sterm, Common.GetUserInfo().UID, SchoolMark);
        }
        catch (Exception e)
        {

            throw;
        }
    }

    private static IList<VItems> ConvertJson(string jsonStr)
    {
        JavaScriptSerializer jss = new JavaScriptSerializer();
        return jss.Deserialize<IList<VItems>>(jsonStr);
    }

    private static string ConvertToCause(int causeID)
    {
        return Container<IGlobalCauseInfoLogic>.Instance.FindById(causeID).CauseName;
    }
}
